# -*- coding: utf-8 -*-
# @Time    : 2022/7/28 14:40
# @Author  : Yanfeng
# @Email   : sxyanfeng2014@163.com
# @File    : tools.py
# @Software: PyCharm
import json
import re
from util.otherutils.functions_control import *

def build_param(obj, expr: str = r'\$\{(.*?)\}'):
    """
    将请求字典中的${}去调用函数得到函数后的值，然后替换请求字段
    :param obj:
    :param expr:
    :return:
    """
    obj = json.dumps(obj)
    # obj = str(obj)
    keys = re.findall(expr,obj)
    for key in keys:
        value = eval(key)
        obj = obj.replace("${"+key+"}",str(value))
    obj = json.loads(obj)
    return obj






if __name__ == '__main__':
    obj = [{'step': '切换到iframe((By.ID,"frmain"))', 'class': 'Sales_manegement_page', 'func': 'swith_to_iframe_frmain', 'value': '${md5("yanfeng")}'}, {'step': '点击销售管理', 'class': 'Sales_manegement_page', 'func': 'click_sales_manegement_buttton', 'value': None}, {'step': '点击ebay Listing管理', 'class': 'Sales_manegement_page', 'func': 'click_ebay_listing_magegement_button', 'value': None}]

    print(build_param(obj))
    print(type(build_param(obj)))
